package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsAdminKeywordServiceImpl implements GoodsAdminKeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public BaseData list(BaseParam baseParam, String keyword, String url) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());//查询之前使用分页
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if((keyword != null && keyword.length()>0)||(url != null && url.length() > 0)){

            if(keyword != null && keyword.length()>0){
                criteria.andKeywordLike("%" + keyword + "%");
            }

            if(url != null && url.length() > 0) {
                criteria.andUrlLike("%" + url + "%");
            }

        }
        else

            criteria.andIdLessThan(20);

        marketKeywordExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());//按规定排序
        List<MarketKeyword> keywordList = marketKeywordMapper.selectByExample(marketKeywordExample);
        //拿total信息
        PageInfo pageInfo = new PageInfo(keywordList);
        long total = pageInfo.getTotal();
        long pages = (total/21)+1;

        return BaseData.list(keywordList,(int)total,baseParam.getPage(),baseParam.getLimit(),(int)pages);

    }

    @Override
    public MarketKeyword create(MarketKeyword marketKeyword) {

        int row = marketKeywordMapper.insertSelective(marketKeyword);
        if(row == 1){
            Integer id = marketKeyword.getId();
            MarketKeyword keywordGoods = marketKeywordMapper.selectByPrimaryKey(id);
            return keywordGoods;
        }else return null;


    }

    @Override
    public MarketKeyword update(MarketKeyword marketKeyword) {
        int row = marketKeywordMapper.updateByPrimaryKey(marketKeyword);
        if(row == 1){
            MarketKeyword keywordGoods = marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
            return keywordGoods;
        }else return null;

    }

    @Override
    public Integer delete(Integer id) {
        if(id > 0) {
            int row = marketKeywordMapper.deleteByPrimaryKey(id);
            return row;
        }return -1;
    }
}
